package syn;

public class Syn2 {

	class Ab implements Runnable {
		synchronized public void run() {
			boolean first = true;
			for (int i = 0; i < 10; i++) {
				if (first == true)
					first = false;
				else
					try {
						wait();
					} catch (InterruptedException e) {
						e.printStackTrace();
						throw new RuntimeException();
					}
				display("ab");
				notifyCd();
			}
		}
		
		synchronized void notifyAb() {
			notify();
		}
	}


	static public void display(String s) {
		for (int i = 0; i < s.length(); i++) {
			System.out.print(s.charAt(i));
			try {
				Thread.sleep(10);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}

	public static void main(String[] args) {
		new Syn2().run();
	}

	synchronized public void run() {
		Ab ab = new Ab();
		new Thread(ab).start();
		for (int i = 0; i < 10; i++) {
			try {
				wait();
			} catch (InterruptedException e) {
				e.printStackTrace();
				throw new RuntimeException();
			}
			display("cd\n");
			ab.notifyAb();
		}
	}
	
	synchronized void notifyCd() {
		notify();
	}

}

